home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11216 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: crl.crl.com!not-for-mail
  2. From: bobfry@crl.com (Robert Fry)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: 12 bit datatype in binary file - misaligned
  5. Date: 22 Mar 1996 09:15:18 -0800
  6. Organization: CRL Dialup Internet Access
  7. Message-ID: <4iun76$bh2@crl.crl.com>
  8. References: <4itqv9$kcq@news.ccit.arizona.edu>
  9. NNTP-Posting-Host: crl.com
  10.  
  11. chrisl@SEDS.LPL.Arizona.EDU (Chris Lewicki) writes:
  12.  
  13. >    I've been scouring the net and the bookstores for the last few
  14. >days trying to figure out how to read a 12-bit data length out of a
  15. >binary file.
  16. >    Here's the setup:  I have a data file that has all lengths of
  17. >fields in it.  There are 1, 2, 3, 4, 6, 8, 12, and 16 bit data types
  18. >all packed in very efficiently.  I've figured out that you can define
  19. >a structure:
  20.  
  21. >struct BitData {
  22. >    unsigned onebit : 1;    
  23. >    unsigned anotheronebit : 1;
  24. >    unsigned fourbit : 4;
  25. >    unsigned twobit : 2;
  26. >    unsigned : 4; /* 4 bits padding */
  27. >    unsigned char eightbit;
  28. >    unsigned twelvebit : 12;
  29. >    unsigned sixteenbit;
  30. >};
  31.  
  32. Sorry, but it generally won't be possible to do a simple read of data 
  33. like this. On a system where bytes are 8 bits, the system will always 
  34. insert an extra four bits padding before the 'eightbit' field. (Well... C 
  35. doesn't /require/ it to do so, but I've never seen it done otherwise. It 
  36. doesn't prevent it, either...).
  37.  
  38. I'm assuming you want to read in an 8-byte value for bitdata (48 bits)?
  39. In that case, your best bet is to read in an 8-byte string, and pull the 
  40. bits out yourself. It's even (mostly) portable.
  41.  
  42. Sorry that's not what you wanted to hear. Good luck.
  43.  
  44.  
  45.